/*
 * EvaluationSource.java - ScriptME
 * 
 * Copyright (c) 2009 Cesar Henriques <cesar at alttab.com.ar>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Based on FESI Project
 * 
 * Contributors:
 * 	Jean-Marc Lugrin - initial API and implementation
 * 	Cesar Henriques <cesar at alttab.com.ar> - J2ME Porting and Extensions
 */
package org.scriptme.ecmascript.interpreter;

/**
 * Abstract declaration of an evaluation source. An evaluation source is used to
 * describe the source of a program for error messages and debugging purpose.
 */
public abstract class EvaluationSource {

	/** The end of line string for this machine. */
	protected String eol = System.getProperty("line.separator") == null ? "\n"
			: System.getProperty("line.separator");

	/** The previous source in case of multi-level evaluation. */
	protected EvaluationSource previousSource = null;

	/**
	 * Create a evaluation source linked to the previous source (which can be
	 * null).
	 * 
	 * @param previousSource
	 *            the previous source
	 */
	public EvaluationSource(EvaluationSource previousSource) {
		super();
		this.previousSource = previousSource;
	}

	/**
	 * Return a description of the evaluation source. Must be implemented for
	 * each specific evaluation source.
	 * 
	 * @return the evaluation source text
	 */
	abstract protected String getEvaluationSourceText();

	/**
	 * Display the description of the evaluation source.
	 * 
	 * @return the string
	 */
	public String toString() {
		return getEvaluationSourceText();
	}

	/**
	 * Return an evaluation source number if defined.
	 * 
	 * @return -1 if not defined
	 */
	public int getLineNumber() {
		if (previousSource != null) {
			return previousSource.getLineNumber();
		}
		return -1; // undefined
	}
}
